/**
 * @file Driver_Button.c
 * @brief 
 * @author 2bam 
 * @version 1.0
 * @date 2024-12-09
 * @copyright Copyright (c) 2024  NULL
 * 
 * @change log:
 * Date           Version Author   Describe
 * 2024-12-09     1.0     2bam     first version
 */


/* Includes -------------------------------------------------------------------*/
#include "config.h"
/* define ---------------------------------------------------------------------*/

/* typedef --------------------------------------------------------------------*/
typedef enum{
    UP = 0,
    DOWN,
    CONFIRM,
    BACK,
    BUTTON_MAX
}button_num;

/* variables ------------------------------------------------------------------*/
struct Button button[BUTTON_MAX];
struct rt_thread button_thread;
static uint8_t button_stack[1024];

rt_bool_t key_up = RT_FALSE;
rt_bool_t key_down = RT_FALSE;
rt_bool_t key_confirm = RT_FALSE;
rt_bool_t key_back = RT_FALSE;

/* Private function prototypes ------------------------------------------------*/
static void ButtonCheckEntry(void *para);



/**
 * @brief 按钮单击事件
 * @param  btn         My Param doc
 * @date 2024-12-09
 * @author 2bam 
 */
void Callback_SINGLE_CLICK_Handler(void* btn)
{
    if(btn == &button[UP])
    {
        key_up = RT_TRUE;
        rt_kprintf("key up\n");
    }
    else if(btn == &button[DOWN])
    {
        key_down = RT_TRUE;
        rt_kprintf("key down\n");
    }
    else if(btn == &button[CONFIRM])
    {
        key_confirm = RT_TRUE;
        rt_kprintf("key confirm\n");
    }
    else if(btn == &button[BACK])
    {
        key_back = RT_TRUE;
        rt_kprintf("key back\n");
    }
    else
    {
        return;
    }
}


uint8_t read_button_up_gpio(void *btn)
{
    return (uint8_t)HAL_GPIO_ReadPin(KEY_UP_GPIO_Port, KEY_UP_Pin);
}

uint8_t read_button_down_gpio(void *btn)
{
    return (uint8_t)HAL_GPIO_ReadPin(KEY_DOWN_GPIO_Port, KEY_DOWN_Pin);
}

uint8_t read_button_confirm_gpio(void *btn)
{
    return (uint8_t)HAL_GPIO_ReadPin(KEY_CONFIRM_GPIO_Port, KEY_CONFIRM_Pin);
}

uint8_t read_button_back_gpio(void *btn)
{
    return (uint8_t)HAL_GPIO_ReadPin(KEY_BACK_GPIO_Port, KEY_BACK_Pin);
}


/**
 * @brief 
 * @date 2024-12-09
 * @author 2bam 
 */
int buttons_register(void)
{
    rt_err_t ret;

    button_init(&button[UP], read_button_up_gpio, 0);
    button_init(&button[DOWN], read_button_down_gpio, 0);
    button_init(&button[CONFIRM], read_button_confirm_gpio, 0);
    button_init(&button[BACK], read_button_back_gpio, 0);

    button_attach(&button[UP], SINGLE_CLICK, Callback_SINGLE_CLICK_Handler);
    button_attach(&button[DOWN], SINGLE_CLICK, Callback_SINGLE_CLICK_Handler);
    button_attach(&button[CONFIRM], SINGLE_CLICK, Callback_SINGLE_CLICK_Handler);
    button_attach(&button[BACK], SINGLE_CLICK, Callback_SINGLE_CLICK_Handler);
    
    button_start(&button[UP]);
    button_start(&button[DOWN]);
    button_start(&button[CONFIRM]);
    button_start(&button[BACK]);

    ret = rt_thread_init(&button_thread,
                        "button thread",
                        ButtonCheckEntry, NULL,
                        button_stack, sizeof(button_stack),
                        RT_THREAD_PRIORITY_BUTTON, 5);
    rt_thread_startup(&button_thread);
    return ret;
}
INIT_APP_EXPORT(buttons_register);

/**
 * @brief 
 * @param  para        My Param doc
 * @date 2024-12-15
 * @author 2bam 
 */
static void ButtonCheckEntry(void *para)
{
    while (1)
    {
        button_ticks();
        rt_thread_mdelay(TICKS_INTERVAL);
    }
}


